package com.invoicing.controller;

import com.invoicing.MainController;
import com.invoicing.Service.CommodityService;
import com.invoicing.Service.TypeService;
import com.invoicing.Service.VendService;
import com.invoicing.dao.CommodityMapper;
import com.invoicing.dao.VendMapper;
import com.invoicing.dao.VendorItemMapper;
import com.invoicing.entity.*;
import com.invoicing.view.InputView;
import com.invoicing.view.MenuView;
import de.felixroske.jfxsupport.FXMLController;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.ResourceBundle;
import java.util.List;

@FXMLController
public class VendController implements Initializable {

    int current_id = 0;
    private List<VendorItem>  vendorItemList = new ArrayList<VendorItem>();
    private ObservableList<CommodityData> data = FXCollections.observableArrayList();
    final ToggleGroup group = new ToggleGroup();
    int choose = 0;
    float sellPrice = 0;
    int num = 0;
    int typeId = 0;






    @Autowired
    private VendService vendService;
    @Autowired
    private CommodityService commodityService;
    @Autowired
    private CommodityMapper commodityMapper;
    @Autowired
    private VendMapper vendMapper;
    @Autowired
    private VendorItemMapper vendorItemMapper;

    @Autowired
    private TypeService typeService;


    @FXML
    private TextField vend_name;

    @FXML
    private TextField vend_price;

    @FXML
    private TextField vend_count;

    @FXML
    private RadioButton one;

    @FXML
    private RadioButton two;

    @FXML
    private RadioButton three;

    @FXML
    private RadioButton four;

    @FXML
    private RadioButton five;

    @FXML
    private RadioButton six;

    @FXML
    private TableView table;

    @FXML
    private TableColumn name;

    @FXML
    private TableColumn price;

    @FXML
    private TableColumn count;

    @FXML
    private TableColumn type;

    @FXML
    private TableColumn vend;

    @FXML
    private TextField newPrice;

    @FXML
    private TableColumn vend_num;










    @FXML
    private void sure(){

        Alert alert = new Alert(Alert.AlertType.WARNING);
        sellPrice = 0;
        if(one.isSelected()){
            choose=1;
        }
        else if(two.isSelected()){
            choose=2;
        }
        else if(three.isSelected()){
            choose=3;
        }
        else if(four.isSelected()){
            choose=4;
        }
        else if(five.isSelected()){
            choose=5;
        }
        else if(six.isSelected()){
            choose=6;
        }
        else{
            alert.setTitle("提示");
            alert.setContentText("请选择商品类型！");
            alert.show();
        }
        if(vend_name.getText().length()>20){
            alert.setTitle("错误");
            alert.setContentText("商品名不能超过20个字符！");
            alert.show();
            vend_name.clear();
        }
        else if(choose!=0&&vend_name.getText().length()!=0&&vend_price.getText().length()!=0&&vend_count.getText().length()!=0){

            if(newPrice.getText().length()!=0){
                sellPrice = Float.parseFloat(newPrice.getText());
            }
            num = num +1;
            Commodity commodity = new Commodity();
            commodity.setName(vend_name.getText());
            commodity.setPrice(sellPrice);//售价
            commodity.setStock(Integer.parseInt(vend_count.getText()));
            commodity.setTypeId(choose);
            CommodityData commodityData = new CommodityData(commodity,num,typeService.findById(commodity.getTypeId()).getTypeName(),Float.parseFloat(vend_price.getText()),commodity.getStock(),0,0);
            data.add(commodityData);
            table.setItems(data);

        }
        else {
            alert.setTitle("提示");
            alert.setContentText("请填写完整商品信息！");
            alert.show();
        }
        vend_name.clear();
        vend_price.clear();
        vend_count.clear();
        newPrice.clear();
        group.selectToggle(null);

    }
    @FXML
    private void setInput(){
        float total = 0;
        Alert alert = new Alert(Alert.AlertType.INFORMATION);

        Vend vend = new Vend();
        Date date = new Date();
        vend.setVendTime(date);
        vendMapper.insertVend(vend);//插入进货单

        current_id = vendService.selectMax();



        for(int i=0;i<data.size();i++){
            typeId = 0;
            if(commodityService.findByName(data.get(i).getName())==null){//新商品
                Commodity commodity = new Commodity();
                commodity.setName(data.get(i).getName());
                commodity.setPrice(data.get(i).getPrice());
                commodity.setTypeId(choose);
                commodityService.insertNew(commodity);
            }




            VendorItem vendorItem = new VendorItem();
            vendorItem.setVendId(current_id);
            vendorItem.setVendNum(i+1);
            vendorItem.setId(commodityService.findByName(data.get(i).getName()).getId());
            vendorItem.setVendUnivalence(data.get(i).getVend_price());
            vendorItem.setVendCount(data.get(i).getVend_count());
            vendorItemList.add(vendorItem);//到列
            total = total+data.get(i).getPrice()*data.get(i).getVend_count();//进货总价

            commodityService.updateStock(vendorItem.getId(),vendorItem.getVendCount()+commodityService.findCommodity(vendorItem.getId()).getStock());//更新数量

            if("零食".equals(data.get(i).getType())){
                typeId = 1;
            }
            else if("饮料".equals(data.get(i).getType())){
                typeId = 2;
            }
            else if("文具".equals(data.get(i).getType())){
                typeId = 3;
            }
            else if("调料".equals(data.get(i).getType())){
                typeId = 4;
            }
            else if("食物".equals(data.get(i).getType())){
                typeId = 5;
            }
            else {
                typeId = 6;
            }
            commodityService.updateTypeId(vendorItem.getId(),typeId);

            if(data.get(i).getPrice()!=0){
                commodityService.updatePrice(vendorItem.getId(),data.get(i).getPrice());
            }


        }


        vendorItemMapper.insertVendorItem(vendorItemList);//插入进货记录




        vendService.updateCost(current_id,total);//更新进货花费
        alert.setTitle("提示");
        alert.setContentText("进货成功！进货单号为："+current_id);
        alert.show();
        sellPrice =0;//新商品的售价
        data.clear();
        table.setItems(data);
        vendorItemList.clear();
        num = 0;
    }

    @FXML
    private void back(){


        data.clear();
        table.setItems(data);
        MainController.showView(MenuView.class);
        num = 0;

    }
    public void init(){

        one.setToggleGroup(group);
        two.setToggleGroup(group);
        three.setToggleGroup(group);
        four.setToggleGroup(group);
        five.setToggleGroup(group);
        six.setToggleGroup(group);
        name.setCellValueFactory(new PropertyValueFactory<CommodityData, String>("name"));
        type.setCellValueFactory(new PropertyValueFactory<CommodityData, String>("type"));
        price.setCellValueFactory(new PropertyValueFactory<CommodityData, Float>("vend_price"));
        count.setCellValueFactory(new PropertyValueFactory<CommodityData, Integer>("vend_count"));
        vend_num.setCellValueFactory(new PropertyValueFactory<CommodityData, Integer>("num"));
    }
    public void initialize(URL location, ResourceBundle resources) {

        init();


    }
}
